package com.dom4j;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * @author 作者：zhaosj
 * @version 创建时间：Nov 9, 2009 6:39:21 PM 类说明
 */

public class UrlConnection {

	public static void main(String[] args) throws IOException {

		try {
			
			//java.net.URL url = new java.net.URL("http://pj.finet.com.cn/chinaFME/GetIndexXml.aspx?type=1");
			//java.net.HttpURLConnection connection = (java.net.HttpURLConnection)url.openConnection();
			//connection.connect();
			//InputStream inputStream = connection.getInputStream() ;
			
			SAXReader saxReader = new SAXReader();
			//Document doc = saxReader.read(inputStream , "UTF-8");
			Document doc = saxReader.read("http://pj.finet.com.cn/chinaFME/GetIndexXml.aspx?type=1") ;

			Element root = doc.getRootElement();
			Element foo;
			
			Iterator it = root.elementIterator("Index") ;
			while (it.hasNext())
			{
				foo = (Element) it.next();
				//Attribute at = foo.attribute("code") ;
				System.out.println("----------------------------------");
				System.out.println("code:"+foo.attribute("code").getValue());
				System.out.println("name:" + foo.elementText("Index_Name"));
				System.out.println("last:" + foo.elementText("Index_Last"));
				System.out.println("change:" + foo.elementText("Index_Change"));
				System.out.println("changeRate:" + foo.elementText("Index_ChangeRate"));
				System.out.println("open:" + foo.elementText("Index_Open"));
				System.out.println("high:" + foo.elementText("Index_High"));
				System.out.println("low:" + foo.elementText("Index_Low"));
				System.out.println("preclose:" + foo.elementText("Index_Preclose"));
				System.out.println("turnover:" + foo.elementText("Index_Turnover"));
				System.out.println("dateTime:" + foo.elementText("Index_DateTime"));
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	public static void main0(String []agrs)
	{
		Connection newCon = null;
		Statement newst = null;
		Statement newst2 = null;
		ResultSet newrs = null;
		ResultSet newrs2 = null;
		
		String mobileBrand = "" ;
		String mobileName = "" ;
		String mobileModel = "" ;
		long mobileId = -1L ;
		
		try {
			newCon = ConnUtil.getConnection("localOracle");
			newst = newCon.createStatement();
			newst2 = newCon.createStatement();
			
			String tmpSql = "select distinct(m.mobile_brand) ,d.key_name from mobile_info m ,dictionary d where m.mobile_brand = d.key_code" ;
			newrs = newst.executeQuery(tmpSql);
			while(newrs.next())
			{
				mobileBrand = newrs.getString(1) ;
				mobileName = newrs.getString(2) ;
				
				System.out.println(mobileName +":"+mobileBrand);
				
				String maxDateSql = "select d.key_name,m.mobile_brand,m.mobile_modle,m.mobile_id " 
					+ " from mobile_info m ,dictionary d "
					+ " where m.mobile_brand = d.key_code "
					+ " and m.mobile_brand = '" + mobileBrand + "'"
					+ " order by m.mobile_id" ;
				
				newrs2 = newst2.executeQuery(maxDateSql);

				StringBuffer sb = new StringBuffer() ;
				while (newrs2.next()) {
					
					mobileModel = newrs2.getString(3) ;
					mobileId	= newrs2.getLong(4) ;
					
					sb.append(mobileModel + "," + mobileId + ";") ;
				}
				
				String str = sb.toString() ;
				if (str.endsWith(";"))
					str = str.substring(0 , str.length() - 1) ;
				
				System.out.println(str);
				
			}

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace() ;
		} finally {
			try {
				if (newrs != null)
					newrs.close();
				if (newst != null)
					newst.close();
				if (newrs2!= null)
					newrs2.close();
				if (newst2 != null)
					newst2.close();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
	}

}
